/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package JET.mygui;

import JET.Centers.Graphics.GUICenter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import org.w3c.dom.*;


/**
 *
 * @author 057Ry
 */
public class UtilTabsPane extends TabsPane {

    public interface UtilTabsPaneTab {
        // Returns individual id of specified utils tab (name of a tab)
        public GUICenter.UtilTabs getId();
        // States if this util tab should be enclosed in JScrollPane
        public boolean isScrollable();
    }

    protected static BufferedImage tabActiveLeftImg, tabActiveCenterImg, tabActiveRightImg,
                                   tabActiveNofocusLeftImg, tabActiveNofocusCenterImg, tabActiveNofocusRightImg,
                                   tabNormalLeftImg, tabNormalCenterImg, tabNormalRightImg,
                                   tabNormalMouseOverLeftImg, tabNormalMouseOverCenterImg, tabNormalMouseOverRightImg,
                                   tabIcon;

    public UtilTabsPane(String name) {
        super(name);
        header = createNewTabsPaneHeader();
        setHeader(header);

        // Load static images
        try {
            // Active tab
            tabActiveLeftImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_active_left.png"));
            tabActiveCenterImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_active_center.png"));
            tabActiveRightImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_active_right.png"));
            // Active without of focus tab
            tabActiveNofocusLeftImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_active_nofocus_left.png"));
            tabActiveNofocusCenterImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_active_nofocus_center.png"));
            tabActiveNofocusRightImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_active_nofocus_right.png"));
            // Normal tab
            tabNormalLeftImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_normal_left.png"));
            tabNormalCenterImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_normal_center.png"));
            tabNormalRightImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_normal_right.png"));
            // Normal tab with mouse focus
            tabNormalMouseOverLeftImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_normal_mouseover_left.png"));
            tabNormalMouseOverCenterImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_normal_mouseover_center.png"));
            tabNormalMouseOverRightImg = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_normal_mouseover_right.png"));

            tabIcon = ImageIO.read(UtilTabsPane.this.getClass().getResource("images/utilTab_icon.png"));
        } catch (IOException e) { e.printStackTrace(); }
    }

    @Override
    protected TabsPaneHeader createNewTabsPaneHeader() {
        return new UtilsTabsPaneHeader();
    }

    ////////////////////////////////////////////////////////////////////////////
    //                         X M L   S T U F F                              //
    ////////////////////////////////////////////////////////////////////////////

    @Override
    public Element getXMLElement(Document doc) {
        Element root = doc.createElement("utilTabsPane");

        // All tabs should be UtilTabsPaneTab istances
        for(JComponent comp : tabs) {
            UtilTabsPaneTab tab = (UtilTabsPaneTab) MyGuiHelper.getComponentAfterScrollPane(comp);
            Element el = doc.createElement("tab");
            el.setAttribute("type", "util");
            el.setAttribute("id", ""+tab.getId());
            root.appendChild(el);
        }

        return root;
    }


    ////////////////////////////////////////////////////////////////////////////
    //                      H E A D E R   C L A S S                           //
    ////////////////////////////////////////////////////////////////////////////

    protected class UtilsTabsPaneHeader extends TabsPane.TabsPaneHeader implements ImageObserver {

        @Override
        public void paintComponent(Graphics g){
            //super.paintComponent(g);
            //fillBackground(g);
            
            Graphics2D g2d = (Graphics2D)g;
            if (tabs.size()>0) {
                double dx = (double)getWidth()/(double)tabs.size();
                for(int i=0; i<tabs.size(); i++) {
                    // Tab body
                    int x1      = (int)( i*dx ),
                        y1      = (int) 0,
                        x2      = (int)(x1+dx) + (i==tabs.size()-1 ? 0 : 1),
                        y2      = (int)(getHeight());

                    int iw = 3; // image width
                    if (i==actualTabIndex) {
                        if (hasFocusFromTabs) {
                            g.drawImage(tabActiveLeftImg, x1, y1, x1+iw, y2, 0, 0, tabActiveLeftImg.getWidth(), tabActiveLeftImg.getHeight(), this);
                            g.drawImage(tabActiveCenterImg, x1+iw, y1, x2-iw, y2, 0, 0, tabActiveCenterImg.getWidth(), tabActiveCenterImg.getHeight(), this);
                            g.drawImage(tabActiveRightImg, x2-iw, y1, x2, y2, 0, 0, tabActiveRightImg.getWidth(), tabActiveRightImg.getHeight(), this);
                        } else {
                            g.drawImage(tabActiveNofocusLeftImg, x1, y1, x1+iw, y2, 0, 0, tabActiveNofocusLeftImg.getWidth(), tabActiveNofocusLeftImg.getHeight(), this);
                            g.drawImage(tabActiveNofocusCenterImg, x1+iw, y1, x2-iw, y2, 0, 0, tabActiveNofocusCenterImg.getWidth(), tabActiveNofocusCenterImg.getHeight(), this);
                            g.drawImage(tabActiveNofocusRightImg, x2-iw, y1, x2, y2, 0, 0, tabActiveNofocusRightImg.getWidth(), tabActiveNofocusRightImg.getHeight(), this);
                        }
                    } else {
                        if (i==mouseOverIndex) {
                            g.drawImage(tabNormalMouseOverLeftImg, x1, y1, x1+iw, y2, 0, 0, tabNormalMouseOverLeftImg.getWidth(), tabNormalMouseOverLeftImg.getHeight(), this);
                            g.drawImage(tabNormalMouseOverCenterImg, x1+2, y1, x2-iw, y2, 0, 0, tabNormalMouseOverCenterImg.getWidth(), tabNormalMouseOverCenterImg.getHeight(), this);
                            g.drawImage(tabNormalMouseOverRightImg, x2-iw, y1, x2, y2, 0, 0, tabNormalRightImg.getWidth(), tabNormalRightImg.getHeight(), this);
                        }
                        else {
                            g.drawImage(tabNormalLeftImg, x1, y1, x1+iw, y2, 0, 0, tabNormalLeftImg.getWidth(), tabNormalLeftImg.getHeight(), this);
                            g.drawImage(tabNormalCenterImg, x1+iw, y1, x2-iw, y2, 0, 0, tabNormalCenterImg.getWidth(), tabNormalCenterImg.getHeight(), this);
                            g.drawImage(tabNormalRightImg, x2-iw, y1, x2, y2, 0, 0, tabNormalRightImg.getWidth(), tabNormalRightImg.getHeight(), this);
                        }
                    }

                    g.drawImage(tabIcon, x1+iw, (getHeight()-tabIcon.getHeight())/2+1, this);

                    // Tab name
                    String name = getTabName(i);
                    if (name==null) name = "No name";
                    Dimension textD = MyGuiHelper.getTextSize(name, g);
                    int ty = getHeight()-(int)((getHeight()-textD.getHeight())/2.)-3;

                    g2d.setColor(Color.BLACK);
                    g2d.drawString(name, x1+8, ty);
                }
            }
            else {
                g2d.setColor(Color.GREEN);
                g2d.draw( new Rectangle2D.Double(0, 0,getWidth()-1, getHeight()-1));
                g2d.drawString("_empty_",0,getHeight()-5);
            }
        }

        ////////////////////////////////////////////////////////////////////////
        //                     D R A G   A N D   D R O P                      //
        ////////////////////////////////////////////////////////////////////////

        @Override
        public boolean accepts(Object o) {
            Component comp = MyGuiHelper.getComponentAfterScrollPane((JComponent) o);
            return comp instanceof UtilTabsPaneTab;
        }
    }
}